﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data;
using NangLuongTieuDiemDTO.DTO;
using NangLuongTieuDiemDAO.DAO;


namespace NangLuongTieuDiemDAO.DataHelper
{
    public class SqlDataHelper
    {
        public static string cnStr = null;
        public static SqlConnection ConnectionData()
        {
            //string cnStr = "Data Source=.;Initial Catalog=NangLuongTieuDiem;Integrated Security=SSPI;";

            string Server = Environment.UserDomainName + @"\SQLExpress;";
            string cnStr = "Server = " + Server + "Database = NangLuongTieuDiem; Trusted_Connection = yes;";

            SqlConnection cn = new SqlConnection(cnStr);
            cn.Open();
            return cn;
        }

        #region "[Backup CSDL]"
        /// <summary>
        /// Người tạo: Cao Hoang An - 1241301
        /// Backup CSDL
        /// </summary>
        /// <param name="_strLocation"></param>
        public static bool Backup(string _strLocation)
        {
            try
            {
                string sqlBackup = "BACKUP DATABASE [NangLuongTieuDiem] TO DISK='" + _strLocation + "'";
                SqlConnection con = ConnectionData();
                SqlCommand cmd = new SqlCommand(sqlBackup, con);
                cmd.ExecuteNonQuery();
                con.Close();
                return true;
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("Backup", ex);
                return false;
            }
        }
        #endregion

        #region "[Restore CSDL]"
        /// <summary>
        /// Người tạo: Cao Hoang An - 1241301
        /// Restore CSDL
        /// </summary>
        /// <param name="_strLocation"></param>
        public static bool Restore(string _strLocation)
        {
            try
            {
                string sqlRestore = "USE master RESTORE DATABASE [NangLuongTieuDiem] FROM DISK='" + _strLocation + "' WITH REPLACE";
                SqlConnection con = ConnectionData();
                SqlCommand cmd = new SqlCommand(sqlRestore, con);
                cmd.ExecuteNonQuery();
                con.Close();
                return true;
            }
            catch(Exception ex)
            {
                InSert_LoiHeThong("Restore", ex);
                return false;
            }
        }
        #endregion

        #region "[Hàm insert lỗi hệ thống]"
        public static void InSert_LoiHeThong(string TenHam, Exception ex)
        {
            LoiHeThongDTO lhtDto = new LoiHeThongDTO();
            lhtDto.TenLoi = ex.Message.ToString();
            lhtDto.NgayTao = DateTime.Now;
            lhtDto.TenHamBiLoi = "SqlDataHelper_" + TenHam;
            lhtDto.SuaLoi = false;
            lhtDto.GhiChu = "";
            LoiHeThongDAO.Insert(lhtDto);

        }
        #endregion
    }
}
